package com.clz.latte.ec.main.sort.content;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.clz.latte.delegate.LatteDaleGate;
import com.clz.latte.ec.R;
import com.clz.latte.ec.R2;
import com.clz.latte.ec.entity.sort.SectionBean;
import com.clz.latte.ec.entity.sort.SortContentListBean;
import com.clz.latte.net.RestClient;
import com.clz.latte.net.callback.ISuccess;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/4/8.
 */

public class ContentDelegate extends LatteDaleGate {
    private static final String CONTENT_ID = "CONTENT_ID";
    private int mContentId = -1;
    @BindView(R2.id.rv_list_content)
    RecyclerView mrclGoods;
    private List<SectionBean> mData = new ArrayList<>();
    private SortContentListAdapter mAdapter;

    public static ContentDelegate newInstance(int contentId) {

        Bundle args = new Bundle();
        args.putInt(CONTENT_ID, contentId);
        ContentDelegate fragment = new ContentDelegate();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle arguments = getArguments();
        mContentId = arguments.getInt(CONTENT_ID);
    }

    @Override
    public Object setLayOut() {
        return R.layout.delegate_list_content;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initRec();
        initData();
    }

    private void initData() {
        RestClient.builder()
                .url("sort_content_list.php?contentId=" + mContentId)
                .loader(getContext())
                .sucess(new ISuccess() {
                    @Override
                    public void onSucess(String response) {
                        JSONObject jsonObject = JSONObject.parseObject(response);
                        final JSONArray json = jsonObject.getJSONArray("data");
                        Gson gson = new Gson();
                        List<SortContentListBean> beans = gson.fromJson(json.toJSONString(), new TypeToken<ArrayList<SortContentListBean>>() {
                        }.getType());
                        for (int i = 0; i < beans.size(); i++) {
                            final SortContentListBean sortContentListBean = beans.get(i);
                            SectionBean sectionBean = new SectionBean(true, sortContentListBean.getSection());
                            mData.add(sectionBean);
                            for (int j = 0; j < sortContentListBean.getGoods().size(); j++) {
                                SectionBean sectionBean2 = new SectionBean(sortContentListBean.getGoods().get(j));
                                mData.add(sectionBean2);
                            }
                        }
                        mAdapter.setNewData(mData);
                    }
                })
                .build()
                .get();
    }

    private void initRec() {
        mrclGoods.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new SortContentListAdapter(R.layout.item_section_content, R.layout.item_section_header, mData);
        mrclGoods.setAdapter(mAdapter);
    }
}
